﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EasySite.Infrastructure.Extensions;

namespace EasySite.Models
{
    public partial class MenuItem
    {
        public string[] Roles { get; set; }
        private readonly List<MenuItem> _children = new List<MenuItem>();
        public List<MenuItem> Children
        {
            get { return _children; }
        }

        public string ActualHref
        {
            get
            {
                if (!string.IsNullOrEmpty(this.Href))
                {
                    return this.Href;
                }
                if (string.IsNullOrEmpty(this.Action))
                {
                    return "/" + this.Controller;
                }
                return string.Format("/{0}/{1}", this.Controller, this.Action);
            }
        }

        public bool Visible
        {
            get
            {
                if(this.NeedAuthentication == null)
                {
                    return true;
                }
                var context = HttpContext.Current;
                if(context.Request.IsAuthenticated)
                {
                    if (this.NeedAuthentication.Value)
                    {
                        return this.Roles.Any(role => context.User.IsInRole(role));
                    }
                    return false;
                }
                return !this.NeedAuthentication.Value;
            }
        }

        private Func<string, string, bool> _isSelectedFuction;
        public Func<string, string, bool> IsSelectedFuction
        {
            get { return _isSelectedFuction ?? (_isSelectedFuction = DefaultIsSelectedFunction()); }
            set { _isSelectedFuction = value; }
        }
        public bool IsSelected
        {
            get
            {
                if (HttpContext.Current == null)
                {
                    return false;
                }
                var routeData = HttpContext.Current.Request.RequestContext.RouteData;
                var controller = routeData.Values["controller"].ToString().ToLower();
                var action = routeData.Values["action"].ToString().ToLower();
                return this.IsSelectedFuction(controller, action);
            }
        }

        public MenuItem(string controller)
            : this(controller, string.Empty)
        {

        }

        public MenuItem(string controller, string displayText)
            : this(controller, string.Empty, displayText)
        {

        }

        public MenuItem(string controller, string action, string displayText)
        {
            this.Controller = controller;
            this.Action = action;
            this.DisplayText = displayText;
        }


        private Func<string, string, bool> DefaultIsSelectedFunction()
        {
            Func<string, string, bool> func = (c, a) =>
            {
                if (!string.IsNullOrEmpty(this.Href))
                {
                    return HttpContext.Current.Request.RawUrl.Eq(this.Href);
                }
                if (!string.IsNullOrEmpty(this.Controller) && !string.IsNullOrEmpty(this.Action))
                {
                    return c.Eq(this.Controller) && a.Eq(this.Action);
                }
                if (string.IsNullOrEmpty(this.Action) && !string.IsNullOrEmpty(this.Controller))
                {
                    return c.Eq(this.Controller);
                }
                return c == "home" && a == "index";
            };
            return func;
        }

    }
}